import { _decorator, Component, Node } from 'cc';
import { ecs } from 'db://oops-framework/libs/ecs/ECS';
import { CCComp } from 'db://oops-framework/module/common/CCComp';
import { CCVMParentComp } from 'db://oops-framework/module/common/CCVMParentComp';
import { Login } from '../Login';
import { oops } from 'db://oops-framework/core/Oops';
import { GameServerConfig } from '../../common/config/GameServerConfig';
import { ShareConfig } from '../../../tsrpc/models/ShareConfig';
import { LoginEvent } from '../LoginEvent';
import { UIID } from '../../common/config/GameUIConfig';
const { ccclass, property } = _decorator;

@ccclass('LoginViewComp')
@ecs.register('LoginView', false)
export class LoginViewComp extends CCVMParentComp {

    data: any = {
        /** 账号 */
        username:"",
        /**密码 */
        pwd:"",
    };
    reset(): void {
        //throw new Error('Method not implemented.');
    }

    onLoad(): void {
        super.onLoad();
        this.on(LoginEvent.Logined, this.onHandler, this);
    }

    protected async start() {
        const res = await this.getEntity<Login>().getGameArea();
        if (res) {
            this.showGameArea();
        } else {
            oops.gui.toast("获取区服列表失败");
        }
    }

    protected showGameArea() {
        console.error("展示区服列表", this.getEntity<Login>().GateModel.area);
    }

    doLogin() {
        // console.error("点击登录", this.data.username, this.data.pwd)
        if (this.getEntity<Login>().GateModel.area == null) {
            oops.gui.toast("获取区服列表失败,无法登录");
            return;
        }
        //选择区服
        const server = this.getEntity<Login>().GateModel.area[0]?.server || "";
        
        this.getEntity<Login>().login(server,this.data.username, this.data.pwd)
    }

    /** 全局消息逻辑处理 */
    private onHandler(event: string, args: any) {
        switch (event) {
            case LoginEvent.Logined:
                oops.gui.remove(UIID.Login);
                break;
        }
    }
    
}


